package com.ieasy.module.system.web.action;

import java.util.List;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ieasy.basic.model.Msg;
import com.ieasy.basic.util.IpUtil;
import com.ieasy.basic.util.cons.Const;
import com.ieasy.basic.util.mail.MailVO;
import com.ieasy.module.common.util.activemq.send.ISendMailService;
import com.ieasy.module.common.web.action.BaseController;
import com.ieasy.module.system.service.IPersonService;
import com.ieasy.module.system.service.IVoteService;
import com.ieasy.module.system.web.form.VoteForm;

@Controller
@RequestMapping("/admin/system/vote")
public class VoteAction extends BaseController {

	@Inject
	private IVoteService voteService ;
	
	@Inject
	private ISendMailService sendMessage ;
	
	@Inject
	private IPersonService personService ;
	
	@RequestMapping("/vote_main_UI.do")
	public String vote_main_UI() {
		return Const.SYSTEM + "vote_main_UI" ;
	}
	
	@RequestMapping("/vote_form_UI.do")
	public String vote_form_UI(VoteForm form, Model model) throws Exception {
		if(null != form.getId() && !"".equals(form.getId())) {
			model.addAttribute("id", form.getId()) ;
		}
		return Const.SYSTEM + "vote_form_UI" ;
	}
	
	@RequestMapping("/vote_add_sub_UI.do")
	public String vote_add_sub_UI(VoteForm form, Model model) throws Exception {
		if(null != form.getId() && !"".equals(form.getId())) {
			model.addAttribute("id", form.getId()) ;
		}
		return Const.SYSTEM + "vote_add_sub_UI" ;
	}
	
	@RequestMapping("/vote_update_sub_UI.do")
	public String vote_update_sub_UI(VoteForm form, Model model) throws Exception {
		if(null != form.getId() && !"".equals(form.getId())) {
			model.addAttribute("id", form.getId()) ;
		}
		return Const.SYSTEM + "vote_update_sub_UI" ;
	}
	
	@RequestMapping("/add.do")
	@ResponseBody
	public Msg add(VoteForm form) throws Exception {
		Msg msg = this.voteService.add(form) ;
		return msg ;
	}
	
	@RequestMapping("/delete.do")
	@ResponseBody
	public Msg delete(VoteForm form) throws Exception {
		Msg msg = this.voteService.delete(form) ;
		return msg ;
	}
	
	@RequestMapping("/update.do")
	@ResponseBody
	public Msg update(VoteForm form) throws Exception {
		Msg msg = this.voteService.update(form) ;
		return msg ;
	}
	
	@RequestMapping("/get.do")
	@ResponseBody
	public VoteForm get(VoteForm form) throws Exception {
		return this.voteService.get(form) ;
	}
	
	@RequestMapping("/tree.do")
	@ResponseBody
	public List<VoteForm> tree(VoteForm form) throws Exception {
		return this.voteService.tree(form.getPid()) ;
	}
	
	@RequestMapping("/doNotNeedAuth_viewVote.do")
	@ResponseBody
	public List<VoteForm> doNotNeedAuth_viewVote(VoteForm form) throws Exception {
		return this.voteService.viewVote(form.getPid()) ;
	}
	
	@RequestMapping("/dotNotNeedSession_listAll.do")
	@ResponseBody
	public List<VoteForm> listAll(VoteForm form) throws Exception {
		return this.voteService.listAll(form.getPid()) ;
	}
	
	@RequestMapping("/dotNotNeedSession_getByPid.do")
	@ResponseBody
	public List<VoteForm> dotNotNeedSession_getByPid(VoteForm form) throws Exception {
		return this.voteService.getByPid(form.getId()) ;
	}
	
	@RequestMapping("/doNotNeedAuth_closeVote.do")
	@ResponseBody
	public Msg doNotNeedAuth_closeVote(VoteForm form) {
		this.voteService.closeVote(form.getId());
		return new Msg(true, "操作成功") ;
	}
	
	/**
	 * 投票
	 * @param form
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/doNotNeedSession_vote.do")
	@ResponseBody
	public Msg doNotNeedSession_vote(VoteForm form, HttpServletRequest request) throws Exception {
		Msg msg = new Msg() ;
		
		form.setIp(IpUtil.getIpAddr(request));
		Boolean vote = this.voteService.vote(form) ;
		
		if(null == vote) {
			msg.setStatus(false);
			msg.setMsg("投票发生错误！！！");
		} else {
			msg.setStatus(vote);
			if(vote) {
				msg.setMsg("投票成功！！！");
			} else {
				msg.setMsg("投票失败！！！");
			}
		}
		
		return msg ;
	}
	
	
	@RequestMapping("/doNotNeedAuth_sort.do")
	@ResponseBody
	public void doNotNeedAuth_sort(int oldSort, int newSort, String id, String pid) {
		this.voteService.updateResetSort(oldSort, newSort, id, pid) ;
	}
	
	@RequestMapping("/vote_view_UI.do")
	public String vote_view_UI(VoteForm form) throws Exception {
		return Const.SYSTEM + "vote_view_UI" ;
	}
	
	
	
	/**
	 * 投票通知邮件
	 * @return
	 */
	@RequestMapping("/vote_mail_UI.do")
	public String vote_mail_UI(VoteForm form,  Model model) {
		if(null != form && null != form.getId()) {
			model.addAttribute("vote", this.voteService.get(form)) ;
		}
		return Const.SYSTEM + "vote_mail_UI" ;
	}
	
	@RequestMapping("/doNotNeedAuth_voteSendMail.do")
	@ResponseBody
	public Msg voteSendMail(VoteForm form) throws Exception {
		
		String allMailAddr = this.personService.getAllMailAddr() ;
		if(null != allMailAddr) {
			
			MailVO mvo = new MailVO() ;
			mvo.setRecipientTO(allMailAddr) ;
			mvo.setRecipientCC("huanglichang@whizen.com") ;
			mvo.setRecipientBCC("yanghaoquan@whizen.com") ;
			mvo.setSubject(form.getSubject()) ;
			mvo.setContent(form.getCtx()) ;
			try {
				this.sendMessage.sendMail(mvo) ;
			} catch (Exception e) {
				e.printStackTrace() ;
			}
			return new Msg(true, "邮件已发送！") ;
		} else {
			return new Msg(false, "邮件发送失败！") ;
		}
		
	}
	

}
